// pages/temperature/temperature.js
import {
  API_TEMPERATURE_SAVEORUPDATE,
  API_TEMPERATURE_GETLIST,
  API_TOURIST_TUBNBUNDLING,
  API_WENGANYUNUSER_LIST
} from '../../api/index'
import {
  debounce
} from "../../utils/util"
const app = getApp()

import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bindFacilityData: {},
    userInfo: {}, // 微信用户数据
    temperatureData: {
      records: [],
    },
    firstTemperatureData: {},
    pagecurrent: 1,
    pagesize: 10,
    services: '',
    flag: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
      bindFacilityData: wx.getStorageSync('bindFacilityData')
    })
    this.getTemperatureList()
  },



  onShow: function () {
    wx.setStorageSync('changeSystem', false)
  },
  onUnload: async function () {
    app.stopDiscovery();
  },
  startNFC: async function () {

    app.onDiscoveredCallBack = async (NFCId) => {
      const {
        bindFacilityData
      } = this.data;
      let isMy = NFCId === bindFacilityData.tpcode;
      isMy = true;
      if (isMy) {
        const tp = await app.startTemperature(wx.getNFCAdapter())
        // const tp = await app.mutipleTemp(wx.getNFCAdapter().getNfcA()); //开始测温;
        console.log('tp:',tp)
        if (tp != null) {
          this.onUpdateSaveor(tp);
        }

      }
    }
    app.startNFCServer();
  },



  // 解绑系统
  onUnDindSystem() {
    Dialog.confirm({
        title: '解绑提示',
        message: '将解绑该游客用户, 确认解绑?',
      })
      .then(() => {
        const params = {
          tpcode: this.data.bindFacilityData.tpcode,
          open_id: wx.getStorageSync('openId')
        }
        API_TOURIST_TUBNBUNDLING(params).then(res => {
          if (res.code == 200) {
            wx.showToast({
              title: '解绑成功',
            })

            setTimeout(() => {
              wx.navigateBack()
            }, 2000)
          }
        })
      })
      .catch(() => {
        // on cancel
      });
  },

  // 修改绑定系统
  onChangeSystem() {
    const params = {
      name: this.data.bindFacilityData.username,
      idcardno: this.data.bindFacilityData.idcardno,
      againtype: '1'
    }
    API_WENGANYUNUSER_LIST(params).then(res => {
      if (res.data instanceof Array) {
        // 保存用户系统列表
        wx.setStorageSync('userSystemList', res.data)
        wx.setStorageSync('changeSystem', true)
        wx.navigateTo({
          url: '/pages/system/system',
        })
      }
    })
  },

  // 刷新当前温度
  onUpdateSaveor(tp) {
    const params = {
      name: this.data.bindFacilityData.username,
      idcardno: this.data.bindFacilityData.idcardno,
      tpcode: this.data.bindFacilityData.tpcode,
      tp: `${tp}`,
      tourist: this.data.bindFacilityData.sort == 8 ? 'Y' : '',
      userid: this.data.bindFacilityData.userid + ''
    }
    API_TEMPERATURE_SAVEORUPDATE(params).then(res => {
      wx.showToast({
        title: '刷新温度成功',
      })
      // this.onCloseBLEConnection()
      this.data.pagecurrent = 1
      this.data.temperatureData = {
        records: [{}],
      }
      this.getTemperatureList()
    })
  },


  // 获取更多
  onLoadMore() {
    this.data.pagecurrent = this.data.pagecurrent + 1
    this.getTemperatureList()
  },

  // 获取温度列表
  getTemperatureList() {
    const params = {
      name: this.data.bindFacilityData.username,
      idcardno: this.data.bindFacilityData.idcardno,
      tpcode: this.data.bindFacilityData.tpcode,
      pagesize: this.data.pagesize,
      pagecurrent: this.data.pagecurrent,
      tourist: this.data.bindFacilityData.sort == 8 ? 'Y' : '',
      userid: this.data.bindFacilityData.userid + ''
    }
    API_TEMPERATURE_GETLIST(params).then(res => {
      if (this.data.pagecurrent == 1) {
        this.setData({
          temperatureData: res.data
        })
      } else {
        this.setData({
          temperatureData: {
            ...res.data,
            records: [...this.data.temperatureData.records, ...res.data.records]
          }
        })
      }
      this.setData({
        firstTemperatureData: this.data.temperatureData.records[0] || {},
        flag: true
      })
    })
  }
})